package com.sos.core.game
{
	import com.sos.utils.ClassUtils;

	public class GameStatus
	{
			
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------              
		
		public static const UNKNOWN:GameStatus				= new GameStatus(00, 	"UNKNOWN",				"Game status unknown");						// no data or not valid, waiting for data
		public static const WAITING:GameStatus				= new GameStatus(05, 	"WAITING",				"Game created, waiting for an opponent");	// game created, waiting for an opponent
		public static const READY:GameStatus				= new GameStatus(10, 	"READY",				"Game created, waiting first resync");		// game init and ready to be started
		public static const SUBSTITUTION:GameStatus			= new GameStatus(20, 	"SUBSTITUTION",			"User must choose player to substitute");	// turn > substitution phase
		public static const RESOLVE_SUBSTITUTION:GameStatus	= new GameStatus(21, 	"RESOLVE_SUBSTITUTION",	"User must choose player to substitute");	// turn > resolve substitution phase
		public static const DISCARD:GameStatus				= new GameStatus(22, 	"DISCARD",				"User must choose action card to discard");	// turn > discard phase
		public static const RESOLVE_DISCARD:GameStatus		= new GameStatus(23, 	"RESOLVE_DISCARD",		"User must choose action card to discard");	// turn > resolve discard phase
		public static const PLAY:GameStatus					= new GameStatus(24, 	"PLAY",					"User must choose action to do");			// turn > action phase
		public static const RESOLVE_PLAY:GameStatus			= new GameStatus(25, 	"RESOLVE_PLAY",			"Waiting for turn resolution");				// turn > resolve action phase 
		public static const RESYNC:GameStatus				= new GameStatus(26,	"RESYNC",				"Game in resyn with server");				// turn > resync with server (status check)
		public static const DONE:GameStatus					= new GameStatus(50, 	"DONE",					"Game ended");
		public static const DESYNC:GameStatus				= new GameStatus(80,	"DESYNC",				"Game client status desync from the server status");
		public static const ERROR:GameStatus				= new GameStatus(90,	"ERROR",				"Game in error");
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<GameStatus>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<GameStatus>();
			_list.push(GameStatus.READY);
			_list.push(GameStatus.PLAY);
			_list.push(GameStatus.RESOLVE_PLAY);
			_list.push(GameStatus.SUBSTITUTION);
			_list.push(GameStatus.DISCARD);
			_list.push(GameStatus.RESYNC);
			_list.push(GameStatus.DONE);
			_list.push(GameStatus.ERROR);
			_list.push(GameStatus.DESYNC);
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _name:String;
		private var _description:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function GameStatus(id:uint, name:String, desc:String)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("GameStatus enum already created!");
			// Init
			_id    			= id;
			_name  			= name;
			_description	= desc;
		}

		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------              
		
		public function get id():uint
		{
			return _id;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		public function get description():String
		{
			return _description;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * Returns the list of the names of 
		 * @return 
		 */
		public static function names():Array
		{
			var list:Array  = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * 
		 * @return 
		 */
		public static function list():Array
		{
			var list:Array = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i]);
			}
			return list;
		}
				
		/**
		 * Search a GameStatus by given id 
		 * @param id Id of the card type to find
		 * @return PlayerGameStatus constant for the given id, null if no GameStatus
		 * for the given id has been found
		 */             
		public static function getById(id:uint):GameStatus
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("GameStatus for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a GameStatus by given name 
		 * @param name Name of the player GameStatus to find
		 * @return PlayerGameStatus constant for the given id, null if no GameStatus
		 * for the given name has been found
		 */             
		public static function getByName(name:String):GameStatus
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("GameStatus for name '" + name + "' unknown!");
		}
		
	}
}